package com.eastfair.projectcore.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.projectcore.dto.FunctionDTO;
import com.eastfair.projectcore.entity.Dictionary;
import com.eastfair.projectcore.dto.DictionarySaveDTO;
import com.eastfair.projectcore.dto.DictionaryUpdateDTO;
import com.eastfair.projectcore.dto.DictionaryPageQuery;
import com.eastfair.projectcore.service.DictionaryService;

import java.io.Serializable;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.projectcore.vo.FunctionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.eastfair.boot.request.PageParams;


/**
 * <p>
 * 前端控制器
 * 字典项
 * </p>
 *
 * @author ligang
 * @date 2021-11-01
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/dictionary")
@Api(value = "Dictionary", tags = "字典项")
public class DictionaryController extends SuperController<DictionaryService, Long, Dictionary, DictionaryPageQuery, DictionarySaveDTO, DictionaryUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<Dictionary> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<Dictionary> dictionaryList = list.stream().map((map) -> {
            Dictionary dictionary = Dictionary.builder().build();
            //TODO 请在这里完成转换
            return dictionary;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(dictionaryList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<DictionaryPageQuery> params) {
        DictionaryPageQuery pageQuery = params.getModel();
        //分页处理
    }


    /**
     * 查询字典项
     *
     * @param type 菜单id
     * @return 查询结果
     */
    @ApiOperation(value = "查询字典项类型", notes = "查询字典项类型")
    @GetMapping("/queryDict")
    public R<List<Dictionary>> queryDict(String type) {
        return success(baseService.queryDict(type));
    }


    /**
     * 分组查询类型
     *
     * @return 查询结果
     */
    @ApiOperation(value = "分组查询类型", notes = "分组查询类型")
    @GetMapping("/queryDictGroupType")
    public R<List<Dictionary>> queryDictGroupType() {
        return success(baseService.queryDictGroupType());
    }

    @GetMapping("/findByIds")
    public Map<Serializable, Object> findUserByIds(@RequestParam(value = "ids") Set<Serializable> ids) {

        return baseService.findByIds(ids);
    }


    /**
     * 测试
     *
     * @param functionDTO 事件实体
     * @return 查询结果
     */
    @ApiOperation(value = "测试", notes = "测试")
    @PostMapping("/test")
    public R<List<FunctionVO>> test(@RequestBody FunctionDTO functionDTO) {
        String test = "{\n" +
                "  \"SUBJECT_TYPE_LIST\": [\n" +
                "    {\n" +
                "      \"name\": \"展商\",\n" +
                "      \"code\": \"ACTOR\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"观众\",\n" +
                "      \"code\": \"VISITOR\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"ENABLED_LIST\": [\n" +
                "    {\n" +
                "      \"name\": \"启用\",\n" +
                "      \"code\": 1\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"禁用\",\n" +
                "      \"code\": 0\n" +
                "    }\n" +
                "  ],\n" +
                "  \"FIELD_GROUP_LIST\": [\n" +
                "    {\n" +
                "      \"name\": \"公司\",\n" +
                "      \"code\": \"COMPANY\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"个人\",\n" +
                "      \"code\": \"PERSONAL\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"总部\",\n" +
                "      \"code\": \"GENNERAL\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"上传\",\n" +
                "      \"code\": \"UPLOAD\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"LANGUAGE_LIST\": [\n" +
                "    {\n" +
                "      \"name\": \"中文\",\n" +
                "      \"code\": \"CN\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"英文\",\n" +
                "      \"code\": \"EN\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"PRE_REGISTRATION_LIST\": [\n" +
                "    {\n" +
                "      \"name\": \"是\",\n" +
                "      \"code\": 1\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"否\",\n" +
                "      \"code\": 0\n" +
                "    }\n" +
                "  ],\n" +
                "  \"DETAILS_LIST\": [\n" +
                "    {\n" +
                "      \"name\": \"是\",\n" +
                "      \"code\": 1\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"否\",\n" +
                "      \"code\": 0\n" +
                "    }\n" +
                "  ],\n" +
                "  \"FIELD_TYPE\": [\n" +
                "    {\n" +
                "      \"name\": \"单行文本\",\n" +
                "      \"code\": \"INPUT\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"下拉列表\",\n" +
                "      \"code\": \"SELECT\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"多行文本\",\n" +
                "      \"code\": \"TEXTAREA\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"上传文件\",\n" +
                "      \"code\": \"FILE\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"级联选择\",\n" +
                "      \"code\": \"CASCADER\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"FIELD_TYPE_ATTRIBUTE\": [\n" +
                "    {\n" +
                "      \"name\": \"字典\",\n" +
                "      \"code\": \"1\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"API\",\n" +
                "      \"code\": \"2\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"自定义\",\n" +
                "      \"code\": \"3\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"SHELVES_TYPE\": [\n" +
                "    {\n" +
                "      \"name\": \"已上架\",\n" +
                "      \"code\": 1\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"已下架\",\n" +
                "      \"code\": 0\n" +
                "    }\n" +
                "  ],\n" +
                "  \"PUBLIC_TYPE\": [\n" +
                "    {\n" +
                "      \"name\": \"公开报名\",\n" +
                "      \"code\": 1\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"不公开报名\",\n" +
                "      \"code\": 0\n" +
                "    }\n" +
                "  ],\n" +
                "  \"FIELD_TYPE_ATTRIBUTE_MAP\": [\n" +
                "    {\n" +
                "      \"name\": \"性别\",\n" +
                "      \"code\": \"SEX\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"职务\",\n" +
                "      \"code\": \"POST\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"网址协议\",\n" +
                "      \"code\": \"URL_PROTOCOL\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"FIELD_TYPE_ATTRIBUTE_API\": [\n" +
                "    {\n" +
                "      \"name\": \"国家\",\n" +
                "      \"code\": \"/project-core/area/getByLevel/4\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"省份\",\n" +
                "      \"code\": \"/project-core/area/getByParent?showType=PROVINCE\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"城市\",\n" +
                "      \"code\": \"/project-core/area/getByParent?showType=CITY\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"展商标签\",\n" +
                "      \"code\": \"/customQuestions/questionSite?showType=ACTOR_LIST\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"展品标签\",\n" +
                "      \"code\": \"/customQuestions/questionSite?showType=PRODUCT_LIST\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"EXAMINE_STATUS\": [\n" +
                "    {\n" +
                "      \"name\": \"审核中\",\n" +
                "      \"code\": \"1\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"已审核\",\n" +
                "      \"code\": \"2\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"未审核\",\n" +
                "      \"code\": \"3\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"TERMINAL_LIST\": [\n" +
                "    {\n" +
                "      \"name\": \"安卓\",\n" +
                "      \"code\": \"1\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"IOS\",\n" +
                "      \"code\": \"2\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"MODULE_LIST\": [\n" +
                "    {\n" +
                "      \"name\": \"banner\",\n" +
                "      \"code\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"按钮\",\n" +
                "      \"code\": \"1\"\n" +
                "    }\n" +
                "  ]\n" +
                "}";

        JSONObject jsonObject = JSONObject.parseObject(test);
        for (String key : jsonObject.keySet()) {
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
                String name = jsonObject1.getString("name");
                String code = jsonObject1.getString("code");
                Dictionary dictionary = new Dictionary();
                dictionary.setType(key);
                dictionary.setCode(code);
                dictionary.setName(name);

                QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda()
                        .eq(Dictionary::getIsDeleted, BusinessConstant.DELETE_NO)
                        .eq(Dictionary::getCode, code)
                .eq(Dictionary::getType,key);
                Dictionary dictionary1 = baseService.getOne(queryWrapper);
                if(dictionary1==null){
                    baseService.save(dictionary);
                }
            }
            

        }

        return success(null);
    }
}
